package com.wolfheader.wolfgame.controller.admin;

import java.security.interfaces.RSAPublicKey;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wolfheader.wolfgame.Message;
import com.wolfheader.wolfgame.controller.BaseController;
import com.wolfheader.wolfgame.dao.admin.AdminDao;
import com.wolfheader.wolfgame.filter.AuthenticationFilter;
import com.wolfheader.wolfgame.service.RsaService;
import com.wolfheader.wolfgame.service.admin.AdminService;
import com.wolfheader.wolfgame.shiro.AuthenticationMessage;

/**
 * @author Administrator
 *
 */
@Controller("adminLoginController")
@RequestMapping("/admin/login")
public class LoginController extends BaseController {

	private static final String template_url = "admin/login";

	@Resource(name = "adminServiceImpl")
	private AdminService adminService;

	@Resource
	AdminDao adminDao;

	@Resource(name = "myRsaServiceImpl")
	private RsaService rsaService;

	
	@RequestMapping(method = RequestMethod.GET)
	public String login(HttpServletRequest request, ModelMap map) {
		// 管理员已登录时，跳转值管理中心
		if (adminService.authorized()) {
			return "redirect:/admin/index";
		}

		RSAPublicKey publicKey = rsaService.generateKey(request);
		// model.addAttribute("captchaId", UUID.randomUUID().toString());
		map.addAttribute("modulus", Base64.encodeBase64String(publicKey.getModulus().toByteArray()));
		map.addAttribute("exponent", Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray()));
		return template_url + "/index";
	}

	/**
	 * 登录
	 */
	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody Message login(HttpServletRequest request) {

		// 登录成功
		String successUrl = (String) request.getAttribute(AuthenticationFilter.SUCCESS_URL);
		if (StringUtils.isNotBlank(successUrl)) {
			return AuthenticationMessage.success("登录成功", successUrl);
		}
		// 登陆失败
		String loginFailureKey = (String) request
				.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
		if (StringUtils.isNotBlank(loginFailureKey)) {
			if (StringUtils.equals(loginFailureKey, "org.apache.shiro.authc.pam.UnsupportedTokenException")) {
				return AuthenticationMessage.error("验证码错误", rsaService.generateKey(request));
			} else if (StringUtils.equals(loginFailureKey, "org.apache.shiro.authc.UnknownAccountException")) {
				return AuthenticationMessage.error("账户或密码错误", rsaService.generateKey(request));
			} else if (StringUtils.equals(loginFailureKey, "org.apache.shiro.authc.DisabledAccountException")) {
				return AuthenticationMessage.error("账户已被禁用", rsaService.generateKey(request));
			} else if (StringUtils.equals(loginFailureKey, "org.apache.shiro.authc.LockedAccountException")) {
				return AuthenticationMessage.error("账户已被锁定", rsaService.generateKey(request));
			} else if (StringUtils.equals(loginFailureKey, "org.apache.shiro.authc.AuthenticationException")) {
				return AuthenticationMessage.error("账户或密码错误", rsaService.generateKey(request));
			}
		}
		return AuthenticationMessage.error("登录失败", rsaService.generateKey(request));
	}

}